package com.usaas.search.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usaas.common.core.domain.AjaxResult;
import com.usaas.search.dto.PlatConfDto;
import com.usaas.search.exception.ExceptionUtil;
import com.usaas.search.mapper.PlatConfMapper;
import com.usaas.search.pojo.PlatConfPojo;
import com.usaas.search.service.IPlatConfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 平台设置Service业务层处理
 *
 * @author Helios
 * @date 2024-08-14
 */
@Slf4j
@Service
public class PlatConfServiceImpl extends ServiceImpl<PlatConfMapper, PlatConfPojo> implements IPlatConfService {

    @Resource
    private PlatConfMapper platConfMapper;

    /**
     * 查询平台设置
     *
     * @param id 平台设置主键
     * @return 平台设置
     */
    @Override
    public PlatConfDto queryById(Long id) {
        PlatConfPojo pojo = this.getById(id);
        if (pojo != null) {
            PlatConfDto dto = BeanUtil.copyProperties(pojo, PlatConfDto.class);
            List<PlatConfDto> dtos = ListUtil.of(dto);
            convertList(dtos);
            return dtos.get(0);
        }
        return null;
    }

    /**
     * 查询平台设置列表
     *
     * @param platConfDto 平台设置
     * @return 平台设置
     */
    @Override
    public List<PlatConfDto> queryList(PlatConfDto platConfDto) {
        LambdaQueryWrapper<PlatConfPojo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = this.buildWrapper(queryWrapper, platConfDto);
        if (queryWrapper == null) {
            return null;
        }
        List<PlatConfPojo> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }

        List<PlatConfDto> dtoList = BeanUtil.copyToList(list, PlatConfDto.class);
        convertList(dtoList);
        return dtoList;
    }

    private void convertList(List<PlatConfDto> dtoList) {
        if (CollectionUtil.isEmpty(dtoList)) {
            return;
        }
    }

    /**
     * 新增平台设置
     *
     * @param platConfDto 平台设置
     * @return 结果
     */
    @Override
    @Transactional
    public boolean add(PlatConfDto platConfDto) {

        PlatConfPojo platConfPojo = BeanUtil.copyProperties(platConfDto, PlatConfPojo.class);
        checkIsRepeat(platConfDto);
        return this.save(platConfPojo);
    }

    private void checkIsRepeat(PlatConfDto platConfDto) {
        Long id = platConfDto.getId();
        if (id != null) {
            //修改 不能改key
            if (StrUtil.isNotBlank(platConfDto.getConfigKey())) {
                ExceptionUtil.throwError("编码KEY不允许修改");
            }
        } else {
            //添加
            //判断是否key重复
            LambdaQueryWrapper<PlatConfPojo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PlatConfPojo::getConfigKey, platConfDto.getConfigKey());
            List<PlatConfPojo> list = this.list(wrapper);
            if (CollectionUtil.isNotEmpty(list)) {
                ExceptionUtil.throwError("编码KEY已存在");
            }
        }
    }

    /**
     * 修改平台设置
     *
     * @param platConfDto 平台设置
     * @return 结果
     */
    @Override
    @Transactional
    public boolean update(PlatConfDto platConfDto) {
        PlatConfPojo platConfPojo = BeanUtil.copyProperties(platConfDto, PlatConfPojo.class);
        checkIsRepeat(platConfDto);
        return this.updateById(platConfPojo);
    }

    /**
     * 批量删除平台设置
     *
     * @param ids 需要删除的平台设置主键
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeBatch(List<String> ids) {
        List<Long> idsLong = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return this.removeBatchByIds(idsLong);
    }


    /**
     * 分页查询${subTable.functionName}信息
     *
     * @param platConfDto
     * @return
     */
    @Override
    public AjaxResult queryPage(PlatConfDto platConfDto) {
        Page<PlatConfPojo> page = new Page<>(platConfDto.getPageNum(), platConfDto.getPageSize());
        LambdaQueryWrapper<PlatConfPojo> wrapper = new LambdaQueryWrapper<PlatConfPojo>();
        //此处可做前置判断
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        wrapper = buildWrapper(wrapper, platConfDto);
        if (wrapper == null) {
            return AjaxResult.success(page);
        }
        Page<PlatConfPojo> pageResult = this.page(page, wrapper);
        Page<PlatConfDto> finalResult = BeanUtil.copyProperties(pageResult, Page.class);
        List<PlatConfPojo> records = pageResult.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            List<PlatConfDto> dtos = BeanUtil.copyToList(records, PlatConfDto.class);
            convertList(dtos);
            finalResult.setRecords(dtos);
        }
        return AjaxResult.success(finalResult);
    }

    private LambdaQueryWrapper<PlatConfPojo> buildWrapper(LambdaQueryWrapper<PlatConfPojo> wrapper, PlatConfDto param) {
        log.info("buildPageWrapper param :{}", param);
        if (wrapper == null) {
            return null;
        }
        Long id = param.getId();
        if (id != null) {
            wrapper.eq(PlatConfPojo::getId, id);
        }
        String configKey = param.getConfigKey();
        if (StrUtil.isNotBlank(configKey)) {
            wrapper.eq(PlatConfPojo::getConfigKey, configKey);
        }
        String configLabel = param.getConfigLabel();
        if (StrUtil.isNotBlank(configLabel)) {
            wrapper.eq(PlatConfPojo::getConfigLabel, configLabel);
        }
        String configValue = param.getConfigValue();
        if (StrUtil.isNotBlank(configValue)) {
            wrapper.eq(PlatConfPojo::getConfigValue, configValue);
        }
        String createBy = param.getCreateBy();
        if (StrUtil.isNotBlank(createBy)) {
            wrapper.eq(PlatConfPojo::getCreateBy, createBy);
        }
        Date createTime = param.getCreateTime();
        if (createTime != null) {
            wrapper.eq(PlatConfPojo::getCreateTime, createTime);
        }
        String updateBy = param.getUpdateBy();
        if (StrUtil.isNotBlank(updateBy)) {
            wrapper.eq(PlatConfPojo::getUpdateBy, updateBy);
        }
        Date updateTime = param.getUpdateTime();
        if (updateTime != null) {
            wrapper.eq(PlatConfPojo::getUpdateTime, updateTime);
        }
        String remark = param.getRemark();
        if (StrUtil.isNotBlank(remark)) {
            wrapper.eq(PlatConfPojo::getRemark, remark);
        }
        Long delFlag = param.getDelFlag();
        if (delFlag != null) {
            wrapper.eq(PlatConfPojo::getDelFlag, delFlag);
        }
        log.info("buildPageWrapper result :{}", wrapper);
        return wrapper;
    }
}
